package com.jobanalysis.job_analysis.controller;


import com.jobanalysis.job_analysis.dto.LoginClass;
import com.jobanalysis.job_analysis.dto.LoginState;
import com.jobanalysis.job_analysis.entity.User;
import com.jobanalysis.job_analysis.service.IJobService;
import com.jobanalysis.job_analysis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@RestController
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private IJobService iJobService;


    /**
     * 登录
     *
     * @param username
     * @param password
     * @param response
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public LoginClass login(String username, String password, HttpServletResponse response) {
        LoginState loginState = userService.findByNameAndPassword(username, password, response);

        return new LoginClass(loginState);
    }

    /**
     * 注册
     *
     * @param request
     * @param username
     * @param password
     * @param response
     * @return
     */
    @RequestMapping("/uregister")
    public LoginClass register(HttpServletRequest request, String username, String password, HttpServletResponse response) {
        LoginClass loginClass = userService.save(new User(username, password));
        return loginClass;
    }

    @PostMapping("/insertJobSort")
    public Map insertJobSort(String hdfs_path, String job_classify) {
        Map map = new HashMap();
        int num = iJobService.countJobSortByPathClass(hdfs_path, job_classify);
        if (num > 0) {
            map.put("msg", "数据已经插入过");
            return map;
        }
        iJobService.insertJobSort(hdfs_path, job_classify);
        map.put("status", 200);
        map.put("msg", "插入成功");

        return map;
    }


}
